const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const app = express();
require('dotenv').config();

const cors = require('cors');
// CORS 跨域配置
const corsOptions = {
  origin: [
    'https://jjz.kakako.top',
    'https://ijia.kakako.top',
    'https://kakako.top',
    'http://192.168.200.10:8000',
  ],
};
app.use(cors());

// 启动邮件消费者
const { mailConsumer } = require('./utils/rabbit-mq');
(async () => {
  await mailConsumer();
})();

// 启动定时任务
const initScheduleTasks = require('./tasks');
initScheduleTasks();

const indexRouter = require('./routes/index');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);

// 路由
const routes = require('./config/routes');
app.use(routes);

module.exports = app;
